//
//  ModelClass.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/15.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "ModelClass.h"

@implementation ModelClass
+(ModelClass *)getClassForDic:(NSDictionary *)dic
{
    ModelClass *class = [ModelClass new];
    [class reflectDataFromOtherObject:dic];
    return class;
}

- (NSArray*)propertyKeys

{
    
    unsigned int outCount, i;
    
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    
    NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:outCount];
    
    for (i = 0; i < outCount; i++) {
        
        objc_property_t property = properties[i];
        
        NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        
        [keys addObject:propertyName];
        
    }
    
    free(properties);
    return keys;
    
}



- (BOOL)reflectDataFromOtherObject:(NSObject*)dataSource

{
    
    BOOL ret = NO;
    
    for (NSString *key in [self propertyKeys]) {
        if ([dataSource isKindOfClass:[NSDictionary class]]) {
            ret = ([dataSource valueForKey:key]==nil)?NO:YES;
        }
        else
        {
            ret = [dataSource respondsToSelector:NSSelectorFromString(key)];
        }
        
        if (ret) {
            id propertyValue = [dataSource valueForKey:key];
            if (![propertyValue isKindOfClass:[NSNull class]] && propertyValue!=nil) {
                if ([propertyValue isKindOfClass:[NSNumber class]]) {
                    
                    propertyValue = [NSString stringWithFormat:@"%@",propertyValue];
                }
                [self setValue:propertyValue forKey:key];
            }else
            {
                propertyValue = @"";
                [self setValue:propertyValue forKey:key];
            }
            
        }
        
    }
    
    return ret;
    
}

@end
